package util.nlp;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;

import util.io.FileInput;
import util.parser.GenericEntryParser;

public class HashDictionary {

	private Hashtable<String, LinkedList<String>> dict = new Hashtable<String, LinkedList<String>>();

	
	
	/**
	 * Create inverse dictionary 
	 * 
	 * 
	 * @param dict
	 */
	public HashDictionary(HashDictionary d) {
		Enumeration<String> keys = d.getKeys();

		while (keys.hasMoreElements()) {

			String key = keys.nextElement();

			LinkedList<String> list = d.getEntries(key);

			for (int i = 0; i < list.size(); i++) {

				if (dict.containsKey(list.get(i))) {
					dict.get(list.get(i)).add(key);

				} else {
					LinkedList<String> list1 = new LinkedList<String>();

					list1.add(key);
					dict.put(list.get(i), list1);

				}

			}

		}

	}
	
	/**
	 * 
	 * List with strings in the form key	value	value2
	 * 
	 * @param list
	 */
	public HashDictionary(LinkedList<String> list) {

		for (int i = 0; i < list.size(); i++) {

			String line = list.get(i);

			String t[] = line.split("\t");
	
			for (int j = 1; j < t.length; j++) {

				if (!dict.containsKey(t[0])) {
					
				

					LinkedList<String> li = new LinkedList<String>();
					li.add(t[j]);
					dict.put(t[0], li);

				} else {
					LinkedList<String> li = dict.get(t[0]);
					li.add(t[j]);
				}

			}

		}

	}
	
	
	
	public Enumeration<String> getKeys(){
		
		
		
		return dict.keys();
	}
	
	
	
	
	
	/**
	 * Builds a dictionary based on a input file with the entries formatted as
	 * follow:
	 * 
	 * key 'TAB' value1 TAB value2 .....
	 * 
	 * @param path
	 */
	
	
	public HashDictionary(String path) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			line = line.trim();

			String t[] = line.split("\t");

			LinkedList<String> list = new LinkedList<String>();

			for (int i = 1; i < t.length; i++) {

				list.add(t[i].trim());

			}

			if (t.length > 0) {

				if (!dict.containsKey(t[0])) {
					dict.put(t[0], list);

				} else {
					LinkedList<String> list2 = dict.get(t[0]);
					list2.addAll(list);
				}
			}

			line = in.readString();
		}
	//	in.close();
	}

	public HashDictionary(String path, String DEL, String SEP, int key,
			int[] values) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		// System.out.println(path);

		while (line != null) {

			line = line.trim();

			String t[] = GenericEntryParser.parseEntry(line, SEP, DEL);

			LinkedList<String> list = new LinkedList<String>();

			for (int i = 0; t!=null && i < values.length; i++) {

				if (values[i] < t.length) {

					list.add(t[values[i]].trim());

//					System.out.println(t[key] + " dic \t" + t[values[i]].trim());
				}

			}

			if (t!=null && t.length > key) {

			//	System.out.println(t[key] + "\t" + list);
				
				if (!dict.containsKey(t[key])) {
					dict.put(t[key], list);

					
				
				} else {
					LinkedList<String> list2 = dict.get(t[key]);
					list2.addAll(list);
					
				
				}
			}

			line = in.readString();
		}

	//	in.close();

	}

	public int getSize(){
		
		return dict.size();
		
	}
	public HashDictionary(String path, String DEL, String SEP, String SEP2,
			int key, int[] values) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			line = line.trim();

			String t[] = GenericEntryParser.parseEntry(line, SEP, DEL);

			LinkedList<String> list = new LinkedList<String>();

			for (int i = 0; i < values.length; i++) {

				if (values[i] < t.length) {

					String temp = t[values[i]].trim();
					String tt[] = temp.split(SEP2);
					for (int j = 0; j < tt.length; j++) {
						list.add(tt[j]);
					}

				}

				if (t.length > key) {

					if (!dict.containsKey(t[key])) {
						dict.put(t[key], list);

					} else {
						LinkedList<String> list2 = dict.get(t[key]);
						list2.addAll(list);
					}
				}

				line = in.readString();
			}
		}
		
		in.close();

	}
	
	
	public HashDictionary(String path, String DEL, String SEP, String SEP2,
			int key, int[] values, String filter, int filter_pos) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			line = line.trim();

			String t[] = GenericEntryParser.parseEntry(line, SEP, DEL);

			LinkedList<String> list = new LinkedList<String>();

			for (int i = 0; i < values.length; i++) {

				if (t[filter_pos].contains(filter)) {

					if (values[i] < t.length) {

						String temp = t[values[i]].trim();
						String tt[] = temp.split(SEP2);
						for (int j = 0; j < tt.length; j++) {
							list.add(tt[j]);
						}

					}

					if (t.length > key) {

						if (!dict.containsKey(t[key])) {
							dict.put(t[key], list);

						} else {
							LinkedList<String> list2 = dict.get(t[key]);
							list2.addAll(list);
						}
					}

				}

				line = in.readString();
			}
		}

		in.close();

	}

	public HashDictionary(String path, boolean cvs) {

		FileInput in = new FileInput(path);

		String line = in.readString();

		while (line != null) {

			line = line.trim();

			String t[] = line.split("\t");

			LinkedList<String> list = new LinkedList<String>();

			if(t!=null && t.length>0){
				
				
				if (t[0].startsWith("\"") && t[0].endsWith("\"") ) {

					t[0] = t[0].substring(1, t[0].length() - 1);
					
				}

				
			}
			
			
			for (int i = 1; i < t.length; i++) {

				if (cvs) {
					t[i] = t[i].trim();
					if (t[i].startsWith("\"") && t[i].endsWith("\"")) {

						t[i] = t[i].substring(1, t[i].length() - 1);
						//System.out.println(t[0]+ "\t" + t[i]);
					}

				}

				list.add(t[i].trim());

			}

			if (t.length > 0)
				dict.put(t[0], list);

			line = in.readString();
		}
in.close();
	}
	
	public boolean containsKey(String key) {

		return dict.containsKey(key);

	}

	public LinkedList<String> getEntries(String key) {

		return dict.get(key);

	}

}
